package cn.jluzh.blog.modules.log.aspect;

import cn.jluzh.blog.commons.utils.LogUtil;
import cn.jluzh.blog.commons.utils.RequestHolder;
import cn.jluzh.blog.modules.log.eneity.SysLog;
import cn.jluzh.blog.modules.log.service.impl.LogServiceImpl;
import cn.jluzh.blog.modules.user.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 系统日志，切面处理类
 *
 * @Author scott
 * @email jeecgos@163.com
 * @Date 2018年1月14日
 */
@Aspect
@Component
public class AutoLogAspect {

    @Resource
    private LogServiceImpl logService;

    /**
     * 配置切入点
     */
    @Pointcut("@annotation(cn.jluzh.blog.modules.log.aspect.annotation.AutoLog)")
    public void logPointCut() {
        // 该方法无方法体,主要为了让同类中其他方法使用此切入点
    }

    /**
     * 配置环绕通知,使用在方法logPointcut()上注册的切入点
     * @param joinPoint join point for advice
     */
    @Around("logPointCut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        result = joinPoint.proceed();
        SysLog log = new SysLog();
        log.setLogType("INFO");
        log.setTime(new Date());
        SysUser user = (SysUser)SecurityUtils.getSubject().getPrincipal();
        System.out.println("user"+user.toString());
        HttpServletRequest request = RequestHolder.getHttpServletRequest();
        logService.save(user.getAccount(), LogUtil.getBrowser(request),LogUtil.getIp(request),(ProceedingJoinPoint)joinPoint,log);
        return result;
    }

}
